package com.lisy.demo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lisy.library.R;

public class MainActivity extends Activity {
    private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) this.findViewById(R.id.lv_main);
        mListView.setAdapter(new DemoListAdapter());
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Intent intent = null;
                intent = new Intent(MainActivity.this, demos[arg2].activityClass);
                startActivity(intent);
            }
        });
    }

    private static final DemoInfo[] demos = {
            new DemoInfo(R.string.library_fragment_contatiner, FragmentContatinerActivity.class),
            new DemoInfo(R.string.library_diaglog, DialogActivity.class),
            new DemoInfo(R.string.library_io, IOActivity.class),
    };

    class DemoListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return demos.length;
        }

        @Override
        public Object getItem(int arg0) {
            return demos[arg0];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(MainActivity.this, R.layout.item_list_content, null);
            TextView titleTextView = (TextView) convertView.findViewById(R.id.tv_title);
            titleTextView.setText(demos[position].title);
            return convertView;
        }

    }

    private static class DemoInfo {
        private final int title;
        private final Class<? extends Activity> activityClass;

        public DemoInfo(int title,Class<? extends Activity> activityClass) {
            this.title = title;
            this.activityClass = activityClass;
        }
    }
}
